package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Parking;
import com.lrkj.property.mapper.ParkingMapper;
import com.lrkj.property.service.ICarportService;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.IParkingService;
import com.lrkj.property.vo.ParkingVo;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 车场管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-15
 */
@Service
public class ParkingServiceImpl implements IParkingService {

    @Autowired
    private ParkingMapper parkingMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private ICarportService carportService;


    /**
     * 查询车场管理
     *
     * @param parkingId 车场管理ID
     * @return 车场管理
     */
    @Override
    public Parking selectParkingById(String parkingId) {
        return parkingMapper.selectParkingById(parkingId);
    }

    /**
     * 查询车场管理列表
     *
     * @param parking 车场管理
     * @return 车场管理
     */
    @Override
    public List<Parking> selectParkingList(Parking parking) {
        return parkingMapper.selectParkingList(parking);
    }

    /**
     * 新增车场管理
     *
     * @param parking 车场管理
     * @return 结果
     */
    @Override
    public int insertParking(Parking parking) {
        parking.setParkingId(IdUtils.simpleUUID());
        parking.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(parking);
        return parkingMapper.insertParking(parking);
    }

    /**
     * 修改车场管理
     *
     * @param parking 车场管理
     * @return 结果
     */
    @Override
    public int updateParking(Parking parking) {
        parking.setUpdateTime(DateUtils.getNowDate());
        this.setUnitInfo(parking);
        return parkingMapper.updateParking(parking);
    }


    private void setUnitInfo(Parking bean) {
        Community parentBean = communityService.selectCommunityById(bean.getCommunityId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getCommunityId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getParkingId(), bean.getParkingName(), bean.getCommunityId(), OrgConstants.park, parentBean.getAncestors(), bean.getOrderNum());
    }

    /**
     * 批量删除车场管理
     *
     * @param parkingIds 需要删除的车场管理ID
     * @return 结果
     */
    @Override
    public int deleteParkingByIds(String[] parkingIds) {
        orgTreeService.deleteOrgTreeByIds(parkingIds);
        return parkingMapper.deleteParkingByIds(parkingIds);
    }

    /**
     * 删除车场管理信息
     *
     * @param parkingId 车场管理ID
     * @return 结果
     */
    @Override
    public int deleteParkingById(String parkingId) {
        return parkingMapper.deleteParkingById(parkingId);
    }

    @Override
    public Map<String, Object> getParkingCarport(String id) {
        return parkingMapper.getParkingCarport(id);
    }

    @Override
    public int addParkingNum(ParkingVo parkingVo) {
        this.checkParams(parkingVo);
        //小区下车场最大编码
        int maxParkingNum = parkingMapper.maxParkingNum(parkingVo.getCommunityId());
        for (Integer i = 0; i < parkingVo.getParkingNum(); i++) {

        }
        return 0;
    }

//    //车场工厂
//    private Parking parkingFactory(ParkingVo parkingVo,int parkingNum){
//        Parking bean = new Parking();
//        bean.setParkingId(IdUtils.simpleUUID());
//        bean.set
//
//        return bean;
//    }

    private void checkParams(ParkingVo parkingVo) {
        if (parkingVo.getParkingNum() < 1) {
            throw new BaseException("车场数量必须大于0");
        }
        if (parkingVo.getCarportNum() < 1) {
            throw new BaseException("车位数量必须大于0");
        }
    }

    /**
     * 批量新增车场管理信息
     *
     * @param parkings 车场管理对象
     */
    public int batchAddData(List<Parking> parkings) {
        return parkingMapper.batchAddData(parkings);
    }
}
